<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Maddress extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
       
    }
    public function test()
    {
         die('234');
    }
    /**
     * 
     * @void Country
     * 
     */
    
    public function getAddressDB($number,$offset)
    {        
        $limit = $offset ? intval($offset) : '0';
        $sql  = '';
        $sql = "SELECT r.id, r.`restaurant_id`, r.`address`, r.`phone`, r.`latitude`, r.`longitude`, r.`active`, r.`id_zone`, r.`admin_id`, r.created FROM restaurant_address AS r WHERE 1=1 ";
        
        $keyword = $this->getParamString('keyword');
        if(!in_array($keyword, array(null, ''))){
            $sql .= "AND r.`address` LIKE '%".$keyword."%' ";
        }
        
        $sql .= "ORDER BY r.id DESC";
        $xdata = " LIMIT ".$limit.",".$number." ";
        $xs = $this->db->query($sql.$xdata)->result_object();
        
        foreach($xs AS $el=>$le)
        {
            $xs[$el]->zone = $this->db->query('SELECT rz.`title`, rz.id FROM restaurant_address AS z INNER JOIN zone AS rz ON rz.id = z.id_zone WHERE z.id = '.$le->id)->result_object();
            
            $xs[$el]->restaurant = $this->db->query('SELECT rck.`name`, rck.id  FROM restaurant_address AS z INNER JOIN restaurant AS rck ON rck.id = z.`restaurant_id` WHERE z.id = '.$le->id)->result_object();
        }
        
        $count = $this->db->query($sql)->num_rows();
        return array('data' => $xs, 'count' => $count);   
    }
    
    public function addAddressDb($admin_id,$restaurant,$address,$phone,$latitude,$longitude,$zone)
    {
        $data = array(
            'restaurant_id' => $restaurant,
            'address' => $address,
            'phone' => $phone,
            'latitude' => $latitude,
            'longitude' => $longitude,
            'active' => 1,
            'id_zone' => $zone,
            'admin_id' => $admin_id,
            'created' => date('Y-m-d H:i:s')
        );
        
        $this->db->insert('restaurant_address', $data);
        $idInsert=$this->db->insert_id();
        $this->db->update('options', array('content' => time()), "tags = 'time_update_restaurant'");
        $this->addAddressOtherDb($restaurant, $address, $phone, $latitude, $longitude,$idInsert);
        return 200;
    }
    
     public function addAddressOtherDb($restaurant,$address,$phone,$latitude,$longitude,$idInsert)
    {
        $this->db_b = $this->load->database('wicard', TRUE);
        $restaurant_w=$this->db_b->query("SELECT id FROM restaurant WHERE id_copy = '$restaurant'")->row()->id;
       
        $data = array(
            'restaurant_id' => $restaurant_w,
            'address' => $address,
            'phone' => $phone,
            'latitude' => $latitude,
            'longitude' => $longitude,
            'active' => 1,
            'id_zone' => 0,
            'admin_id' => 14,
            'created' => date('Y-m-d H:i:s'),
            'id_copy'=>$idInsert
        );
        $this->db_b->insert('restaurant_address', $data);
        $this->db_b->update('options', array('content' => time()), "tags = 'time_update_restaurant'");
        return 200;
    }
    public function getThisAddressDb($id)
    {
        $data = $this->db->query("SELECT * FROM restaurant_address WHERE id = ".$id)->row_array();
        return $data;
    }
    public function editAddressDb($admin_id,$restaurant,$address,$phone,$latitude,$longitude,$zone,$id)
    {
        $data = array(
            'restaurant_id' => $restaurant,
            'address' => $address,
            'phone' => $phone,
            'latitude' => $latitude,
            'longitude' => $longitude,
            'active' => 1,
            'id_zone' => $zone,
            'admin_id' => $admin_id,
            'created' => date('Y-m-d H:i:s')
        );
        
        $this->db->update('restaurant_address', $data, "id = ".$id);
        $this->db->update('options', array('content' => time()), "tags = 'time_update_restaurant'");
        $this->editAddressOtherDb($restaurant, $address, $phone, $latitude, $longitude, $id);
        return 200;
    }
    public function editAddressOtherDb($restaurant,$address,$phone,$latitude,$longitude,$id)
    {
        $this->db_b = $this->load->database('wicard', TRUE);
        $restaurant_w=$this->db_b->query("SELECT id FROM restaurant WHERE id_copy = '$restaurant'")->row()->id;
        $idAddress_w=$this->db_b->query("SELECT id FROM restaurant_address WHERE id_copy = '$id'")->row()->id;
        $data = array(
            'restaurant_id' => $restaurant_w,
            'address' => $address,
            'phone' => $phone,
            'latitude' => $latitude,
            'longitude' => $longitude,
            'active' => 1,
//            'id_zone' => $zone,
//            'admin_id' => $admin_id,
            'created' => date('Y-m-d H:i:s')
        );
        
        $this->db_b->update('restaurant_address', $data, "id = ".$idAddress_w);
        $this->db->update('options', array('content' => time()), "tags = 'time_update_restaurant'");
        return 200;
    }
    
    public function deleteAddressDB($id)
    {
        $this->db->query("DELETE FROM restaurant_address WHERE id IN ('" . (is_array($id) ? implode("','", $id) : $id) . "')");
        $this->db->update('options', array('content' => time()), "tags = 'time_update_restaurant'");
        $this->deleteAddressOtherDB($id);
        return 200;
    }  
    public function deleteAddressOtherDB($id)
    {
        $this->db_b = $this->load->database('wicard', TRUE);
//        $id_Other = $this->db_b->query("SELECT id FROM restaurant_address WHERE id_copy IN ('" . (is_array($id) ? implode("','", $id) : $id) . "')")->result_array();
//        foreach ($id_Other as  $value)
//        {
//            $arrayId[]=$value['id'];
//        }
//        $id_Other=$arrayId;
//        $this->db_b->query("DELETE FROM restaurant_address WHERE id IN ('" . (is_array($id_Other) ? implode("','", $id_Other) : $id_Other) . "')");
        $this->db_b->query("UPDATE restaurant_address SET delete_status=1 WHERE id_copy IN ('" . (is_array($id) ? implode("','", $id) : $id) . "')");
        $this->db_b->update('options', array('content' => time()), "tags = 'time_update_restaurant'");
        
        return 200;
    }  
    
    public function getRestaurant()
    {
        $data = $this->db->query('SELECT id, name FROM restaurant ORDER BY id ASC')->result_array();
        return $data;
    }
    public function getZone()
    {
        $data = $this->db->query('SELECT id, title AS name FROM zone ORDER BY id ASC')->result_array();
        return $data;
    }
    
    public function optionRestaurant($id)
    {
        $data = $this->db->query('SELECT id, title FROM country ORDER BY title ASC')->result_object();
        $html = '<option value = "">Chọn quốc gia</option>';
        foreach($data AS $el=>$le)
        {
            if($le->id == $id)
            {
                $selected = 'selected="selected"';
            }else{
                $selected = '';
            }
            $html .= '<option value="'.$le->id.'" '.$selected.'>'.$le->title.'</option>';
        }
        return $html;
    }
    
    public function optionZone($id)
    {
        $data = $this->db->query('SELECT id, title FROM zone ORDER BY title ASC')->result_object();
        $html = '<option value = "">Chọn khu vực</option>';
        foreach($data AS $el=>$le)
        {
            if($le->id == $id)
            {
                $selected = 'selected="selected"';
            }else{
                $selected = '';
            }
            $html .= '<option value="'.$le->id.'" '.$selected.'>'.$le->title.'</option>';
        }
        return $html;
    }
    
    
    
    public function danhmucGroupotp($selected = '')
    {
        $html = '';$selecteds = '';
    	$gRs = $this->db->query("SELECT * FROM restaurant_categories WHERE parent_id = 0 ORDER BY id ASC")->result_array();
        if($gRs)
        {
            $child = '|--';
            foreach ($gRs As $gel)
        	{
					if(is_array($selected))
					{ 
						foreach($selected AS $el=>$le)
						{
							if($le['id'] == $gel['id'])
							{
								$selecteds = 'selected="selected"';
							}
						}
					}
			
					$check_parent = $this->db->query('SELECT id FROM restaurant_categories WHERE parent_id = '.$gel['id'])->num_rows();
					if($check_parent > 0)
					{
						$html .= ' <optgroup label="' . $gel['title'] .'" style="font-weight: bold;color: #16708A">fdasfdsf';
						$html .= $this->subdanhmucGroupotp($selected, $gel['id'], $child);
						$html .= '</optgroup>';
					}else{
						$html .= '<option value="' . $gel['id'] . '" '.$selecteds;
						$html .= '>' . $child .' '. $gel['title'] . '</option>';
						$html .= $this->subdanhmucGroupotp($selected, $gel['id'], $child);
					}
    	    		
        	}
        }
        return $html;
    }
    private function subdanhmucGroupotp($selected, $parent,$child)
    {
        
        $child = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$child;
        $htmls = '';
        $selecteds = '';
        $gGs = $this->db->query("SELECT * FROM restaurant_categories WHERE parent_id = '".$parent."' ORDER BY id ASC")->result_array();
        foreach ($gGs As $key=>$val)
		{
		  
            if(is_array($selected))
            {   
                foreach($selected AS $el=>$le)
                {
                    if($le['id'] == $val['id'])
                    {
                        $selecteds = 'selected="selected"';
                    }
                }
            }
            
            
            $check_parent = $this->db->query('SELECT id FROM restaurant_categories WHERE parent_id = '.$val['id'])->num_rows();
            if($check_parent > 0)
            {
                $htmls .= ' <optgroup label="&nbsp;&nbsp;' . $child .' ' . $val['title'] .'" style="font-weight: bold;color: #16708A">
                ';
                $htmls .= $this->subdanhmucGroupotp($selected, $val['id'], $child);
                $htmls .= '</optgroup>';
            }else{
                //echo $selecteds.'<br />';
                $htmls .= '<option value="' . $val['id'] . '" '.$selecteds;
            	$htmls .= '>' . $child .' '. $val['title'] . '</option>';
                $htmls .= $this->subdanhmucGroupotp($selected, $val['id'], $child);
            }

                
		}
        return $htmls;
    }
    
    
    public function getRestaurantDetailDB($id)
    {
        $sql = "SELECT r.* FROM restaurant AS r WHERE 1=1 ";
        
        $xdata = " AND r.id = ".$id;
        $xs = $this->db->query($sql.$xdata)->row_object();
        $xs->categories = $this->db->query('SELECT rck.id_categories AS id FROM restaurant AS r  INNER JOIN restaurant_categories_key AS rck ON rck.id_restaurant = r.id  WHERE r.id = '.$xs->id)->result_array();
        return $xs;  
    }
    
}
